<?php
namespace YDCMS\core;

class Controller
{
    protected function view($view, $data = [])
    {
        // 将数据变量解压到当前作用域
        extract($data);
        
        // 构建视图文件路径
        $viewFile = APP_PATH . 'views/' . $view . '.php';
        
        if (file_exists($viewFile)) {
            ob_start();
            require $viewFile;
            return ob_get_clean();
        }
        
        throw new \Exception("View file not found: {$view}");
    }

    protected function json($data, $statusCode = 200)
    {
        header('Content-Type: application/json');
        http_response_code($statusCode);
        echo json_encode($data);
    }

    protected function redirect($url)
    {
        header("Location: {$url}");
        exit;
    }
} 